package lib

import (
	"encoding/json"
	"fmt"
	"net/http"

	//"url"
	"io/ioutil"
	"strconv"
	"time"
)

type ClassDowns struct {
	Video struct {
		ID   string `json:"id"`
		Name string `json:"name"`
		URL  string `json:"url"`
		Size int    `json:"size"`
		Md5  string `json:"md5"`
	} `json:"video"`
}

func (this *ClassDowns) Dump(userid, videoid string) (string, error) {

	timeStamp := time.Now().Unix()
	apiKey := "jFvlz9YUyR6JeLveCDopi0ANxypBvt7u"
	prePushUrl := "userid=" + userid + "&videoid=" + videoid + "&time=" + Stamp2Strs(timeStamp) + "&salt=" + apiKey
	publishUrlHash := StrMd5STR(prePushUrl)
	//fmt.Println(publishUrlHash)
	getPulishUrl := "https://spark.bokecc.com/api/video/original?userid=" + userid + "&videoid=" + videoid + "&time=" + Stamp2Strs(timeStamp) + "&hash=" + publishUrlHash
	//fmt.Println(getPulishUrl)
	resp, err := http.Get(getPulishUrl)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	json.Unmarshal(body, this)
	fmt.Println("cc视频请求数据:", this)
	return this.Video.URL, err
}

func Stamp2Strs(stamp int64) string {
	return strconv.FormatInt(stamp, 10)
}
